package persentation.presentStudent;

import java.awt.Font;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;

import vo.courseVO.CourseVO;
import vo.userVO.StudentVO;
import businesslogic.chooseCourseBl.ChooseCourseController;
import businesslogic.courseBl.CourseController;
import businesslogic.userBl.StudentController;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JSeparator;

import dataservice.ResultMessage;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.DefaultComboBoxModel;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import javax.swing.ListSelectionModel;

public class ChooseCoursePanel extends JPanel {
	private ChooseCourseController chooseCourseController;
	private CourseController courseController;
	private StudentController studentController;
	private ArrayList<CourseVO> courseVOs;         //可选课程
	private ArrayList<CourseVO> chooseRecord;
	private JTable table;
	private JTable choosetable;
	private StudentVO studentVO;
	private MyTabbedPane tabbedPane;
	private String []departmentList=new String[40];
	JButton btnNewButton;
	JButton btnNewButton_1;
	JButton btnNewButton_3 ;
	
	/**
	 * Create the panel.
	 */
	public ChooseCoursePanel(ChooseCourseController chooseCourseController,CourseController courseController,
			                StudentController studentController) {
		this.chooseCourseController=chooseCourseController;
		this.courseController=courseController;
		this.studentController=studentController;
		courseVOs=new ArrayList<>();
		try {
			courseVOs=courseController.getSelectableCourses();
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			ExceptionHandler.networkException();
		}
		ArrayList<String> departmentArrayList = null;
		try {
			departmentArrayList = courseController.getAllDepartments();
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			ExceptionHandler.networkException();
		}
		departmentList[0]="全部";
		for (int i = 0; i<departmentArrayList.size(); i++) {
			departmentList[i+1]=departmentArrayList.get(i);
		}
		try {
			studentVO=studentController.getStudentVO();
		} catch (Exception e1) {
			
			e1.printStackTrace();
			ExceptionHandler.networkException();
		}
		
		try {
			chooseRecord=chooseCourseController.getMyTemp();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			ExceptionHandler.networkException();
		}
		
		setSize(800, 600);
		setLayout(null);
		
		btnNewButton = new JButton("选择");
		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRow()>-1) {
					int row=table.getSelectedRow();
					
					if (chooseRecord.contains(courseVOs.get(row))) {
						
						JOptionPane.showMessageDialog(null,"你已经选择了该课程！","失败！",JOptionPane.INFORMATION_MESSAGE);
					}
					else {
						chooseRecord.add(courseVOs.get(row));
						fillTable2();
					}
					
				} 
				else {
					JOptionPane.showMessageDialog(null,"请选择想要选择的课程","失败！",JOptionPane.ERROR_MESSAGE);
				}
				
			}
		});
		btnNewButton.setBounds(53, 450, 93, 23);
		btnNewButton.setEnabled(false);
		add(btnNewButton);
		
		btnNewButton_1 = new JButton("查看详细信息");
		btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRow()>-1) {
					CourseVO courseVO=courseVOs.get(table.getSelectedRow());
					tabbedPane.addOne(courseVO.getCourseName()+" 详细信息",new InfoPanel(courseVO));
				}
				else {
					JOptionPane.showMessageDialog(null,"请选择想查看的课程","失败！",JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnNewButton_1.setBounds(216, 450, 125, 23);
		btnNewButton_1.setEnabled(false);
		add(btnNewButton_1);
	
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(22, 71, 416, 349);
		this.add(scrollPane);
		
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null,null,null,null},
				{null, null,null,null,null}
			},
			new String[] {
					 "课程编号","课程名","任课老师","上课时间","学分"
			}
		) 
		{
			Class[] columnTypes = new Class[] {
				String.class, String.class,String.class,String.class,String.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		table.setBounds(119, 424, 262, 143);
		scrollPane.setViewportView(table);
		table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		table.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				if (table.getSelectedRow()!=-1) {
					btnNewButton.setEnabled(true);
					btnNewButton_1.setEnabled(true);
				}
				else {
					btnNewButton.setEnabled(false);
				}
			}
		});
		

		fillTable();

		
		JScrollPane scrollPane2 = new JScrollPane();
		scrollPane2.setBounds(508, 71, 282, 262);
		this.add(scrollPane2);
		
		choosetable = new JTable();
		choosetable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		choosetable.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		choosetable.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null,null}
				
			},
			new String[] {
					 "课程编号","课程名"
			}
		) 
		{
			Class[] columnTypes2 = new Class[] {
				String.class, String.class
			};
		});
		choosetable.setBounds(119, 424, 262, 143);
		scrollPane2.setViewportView(choosetable);
		choosetable.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				if (choosetable.getSelectedRow()!=-1) {
					btnNewButton_3.setEnabled(true);
				}
				
			}
		});
		fillTable2();
		JButton btnNewButton_2 = new JButton("提交");
		btnNewButton_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (chooseRecord.size()!=0) {
					int i=JOptionPane.showConfirmDialog(null,"确认选课？","确认",JOptionPane.YES_NO_OPTION);
					if(i==JOptionPane.YES_OPTION){
						ResultMessage resultMessage=sendRequest();
						if (resultMessage==ResultMessage.INSERT_SUCCESS) {
							JOptionPane.showMessageDialog(null,"选择成功","成功！",JOptionPane.INFORMATION_MESSAGE);
						}
						else if (resultMessage==ResultMessage.MAX_NUM) {
							JOptionPane.showMessageDialog(null,"你选择的课程超过最大数目","失败！",JOptionPane.ERROR_MESSAGE);
						}
						else if (resultMessage==ResultMessage.INSERT_DEPLICATE_PRIMARY) {
							JOptionPane.showMessageDialog(null,"你已经选择了这门课程","失败！",JOptionPane.ERROR_MESSAGE);
						}
						else {
							JOptionPane.showMessageDialog(null,"选课失败","失败！",JOptionPane.ERROR_MESSAGE);
						}
					}
				}
				else {
					JOptionPane.showMessageDialog(null,"你未选择任何课程！","错误！",JOptionPane.ERROR_MESSAGE);
				}
				
			}
		});
	
		btnNewButton_2.setBounds(534, 359, 93, 23);
		add(btnNewButton_2);
		
		btnNewButton_3 = new JButton("删除\r\n");
		btnNewButton_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (choosetable.getSelectedRow()>-1) {
					quitCourse(choosetable.getSelectedRow());
				}
				else {
					JOptionPane.showMessageDialog(null,"请选择想删除的课程","失败！",JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		btnNewButton_3.setBounds(662, 359, 93, 23);
		btnNewButton_3.setEnabled(false);
		add(btnNewButton_3);
		
		final JComboBox comboBox = new JComboBox();
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange()==ItemEvent.SELECTED) {
					changeCourseList(departmentList[comboBox.getSelectedIndex()]);
				}
			}
		});
		
		
		comboBox.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		comboBox.setModel(new DefaultComboBoxModel(departmentList));
		comboBox.setBounds(114, 30, 200, 21);
		add(comboBox);
		
		JLabel label = new JLabel("院系");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		label.setBounds(41, 20, 56, 41);
		add(label);
	}
	
	public void fillTable(){
		 DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		 tableModel.setRowCount(0);
		 for(CourseVO course:courseVOs){
			 String[] array=new String [5];
			 array[0]=course.getCourseId();
			 array[1]=course.getCourseName();
			 array[2]=course.getTeacherName();
			 array[3]=course.getCourseTime();
			 array[4]=String.valueOf(course.getCredit());
		
			 tableModel.addRow(array);
			 table.invalidate();
			 table.updateUI();
	      }
	} 
	
	public void fillTable2(){
		 DefaultTableModel tableModel = (DefaultTableModel) choosetable.getModel();
		 tableModel.setRowCount(0);
		 for(CourseVO courseVO:chooseRecord){
			 String[] array=new String [2];
			 array[0]=courseVO.getCourseId();
			 array[1]=courseVO.getCourseName();
			 
			 tableModel.addRow(array);
			 table.invalidate();
			 table.updateUI();
		 }
		
	}
	
	public ResultMessage sendRequest(){
		ResultMessage resultMessage=null;
		if (chooseRecord.size()>chooseCourseController.MAX_CHOOSE_NUM) {
			JOptionPane.showMessageDialog(null,"你已选的课程超过最大可选课程数，请更新选择后重新提交！","失败！",JOptionPane.ERROR_MESSAGE);
			return ResultMessage.CHOOSECOURSE_FAIL;
		}
		else {

			try {
				resultMessage=chooseCourseController.chooseCourse(chooseRecord, studentVO);
			} catch (Exception e) {
				ExceptionHandler.networkException();
			}
		}
		return resultMessage;
	}
	
	public void quitCourse(int row){
		chooseRecord.remove(row);
		fillTable2();
	}
	
	public void changeCourseList(String departmentId){
		if (departmentId.equals("全部")) {
			try {
				courseVOs=courseController.getSelectableCourses();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				ExceptionHandler.networkException();
			}
		}
		else {
			try {
				courseVOs=courseController.getSelectableCourse(departmentId);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				ExceptionHandler.networkException();
			}
			
		}
		fillTable();
	}
	
	public void setTabbedPane(MyTabbedPane tabbedPane){
		this.tabbedPane=tabbedPane;
	}
} 
	

